﻿using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using Game.UI.Inner;

namespace Game.UI
{
    /// <summary>
    /// 支持ClickDown/Up编程的Button.
    /// <para>由于方法库与项目分离，所以尽量使用其他项目中的代码或MyInput来AddComponent，而不要直接将此类悬挂到某个游戏物体上</para>
    /// </summary>
    public class GameUIClickable : UnityEngine.UI.Selectable, IPointerDownHandler, IPointerUpHandler ,IPointerClickHandler
	{
        CallbackFunc 
            m_cbOnClick,
            m_cbOnClickDown, 
            m_cbOnClickUp;

        /// <summary>
        /// 添加回调，不清除之前的回调
        /// </summary>
        /// <see cref="SetOnClickListener"/>
        public void AddListener(CallbackFunc cbOnClick, CallbackFunc cbOnClickDown = null, CallbackFunc cbOnClickUp  = null)
        {
            SetOnClickListener(cbOnClick, cbOnClickDown, cbOnClickUp,true);
        }

        /// <summary>
        /// 直接设置回调，将清除之前的回调
        /// </summary>
        /// <param name="cbOnClick"></param>
        public void SetOnClickListener(CallbackFunc cbOnClick)
        {
            m_cbOnClick = cbOnClick;

        }

        /// <summary>
        /// 直接设置回调，将清除之前的回调
        /// </summary>
        /// <param name="cbOnClickDown"></param>
        /// <param name="cbOnClickUp"></param>
        public void SetOnClickListener(CallbackFunc cbOnClickDown , CallbackFunc cbOnClickUp)
        {
            m_cbOnClickDown = cbOnClickDown;
            m_cbOnClickUp = cbOnClickUp;
        }

        /// <summary>
        /// 直接设置回调，将清除之前的回调
        /// </summary>
        public void SetOnClickListener(CallbackFunc cbOnClick, CallbackFunc cbOnClickDown, CallbackFunc cbOnClickUp)
        {
            SetOnClickListener(cbOnClick, cbOnClickDown, cbOnClickUp, false);
        }

        public override void OnPointerUp(PointerEventData data)
        {
            //回调
            if (interactable && m_cbOnClickUp != null)
            {
                m_cbOnClickUp();
            }
        }


        public override void OnPointerDown(PointerEventData data)
        {
            //回调：
            if (interactable && m_cbOnClickDown != null)
            {
                m_cbOnClickDown();
            }

        }

        public void OnPointerClick(PointerEventData eventData)
        {
            if (interactable && m_cbOnClick != null)
            {
                m_cbOnClick();
            }
        }
        
        /// <summary>
        /// 清除设置的回调
        /// </summary>
        public void RemoveListeners()
        {
            m_cbOnClickDown = null;
            m_cbOnClickUp = null;
            m_cbOnClick = null;

        }

        /// <summary>
        /// 重新设置回调或者增加回调
        /// </summary>
        /// <param name="cbOnClick"></param>
        /// <param name="cbOnClickDown"></param>
        /// <param name="cbOnClickUp"></param>
        /// <param name="isAdd"></param>
        private void SetOnClickListener(CallbackFunc cbOnClick, CallbackFunc cbOnClickDown, CallbackFunc cbOnClickUp, bool isAdd)
        {
            if (isAdd)
            {
                if(cbOnClick!=null)
                {
                    m_cbOnClick += cbOnClick;
                }

                if(cbOnClickDown!=null)
                {
                    m_cbOnClickDown += cbOnClickDown;
                }

                if(cbOnClickUp!=null)
                {
                    m_cbOnClickUp += cbOnClickUp;
                }
            }
            else
            {
                m_cbOnClick = cbOnClick;
                m_cbOnClickDown = cbOnClickDown;
                m_cbOnClickUp = cbOnClickUp;
            }
        }

    }
}